package tk.uodrad.apex.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.pojo.dto.ChargeDetailsAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.ChargeDetailsUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.AssetEquipment;
import tk.uodrad.apex.project.pojo.entity.ChargeDetails;
import tk.uodrad.apex.project.pojo.vo.AssetEquipmentListVO;
import tk.uodrad.apex.project.pojo.vo.ChargeDetailsAmountVO;
import tk.uodrad.apex.project.pojo.vo.ChargeDetailsDashboardVO;
import tk.uodrad.apex.project.pojo.vo.ChargeDetailsListVO;
import tk.uodrad.apex.project.service.ChargeDetailsService;
import tk.uodrad.apex.project.mapper.ChargeDetailsMapper;
import org.springframework.stereotype.Service;
import tk.uodrad.apex.project.utils.InitRandomCode;
import tk.uodrad.apex.project.web.ServiceCode;

import java.util.List;

/**
* @author Administrator
* @description 针对表【charge_details_management】的数据库操作Service实现
* @createDate 2023-04-06 19:35:55
*/
@Slf4j
@Service
public class ChargeDetailsServiceImpl extends ServiceImpl<ChargeDetailsMapper, ChargeDetails>
    implements ChargeDetailsService{

    @Autowired
    private ChargeDetailsMapper chargeDetailsMapper;

    @Override
    public void save(ChargeDetailsAddNewDTO chargeDetailsAddNewDTO) {
        //新建一个chargeDetails对象并将传入的DTO复制给chargeDetails
        ChargeDetails chargeDetails = new ChargeDetails();
        BeanUtils.copyProperties(chargeDetailsAddNewDTO, chargeDetails);
        //插入数据，并判断是否插入成功
        int rows = chargeDetailsMapper.insert(chargeDetails);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public ChargeDetails selectById(Long id) {
        ChargeDetails queryResult=chargeDetailsMapper.selectById(id);
        if (queryResult == null) {
            String message = "查询收费明细失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public void updateById(ChargeDetailsUpdateDTO chargeDetailsUpdateDTO) {
        ChargeDetails chargeDetailsById = chargeDetailsMapper.selectById(chargeDetailsUpdateDTO.getId());
        if (chargeDetailsById == null) {
            String message = "修改数据失败，当前数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //创建一个chargeDetails对象，将传入的DTO复制给chargeDetails对象
        ChargeDetails chargeDetails = new ChargeDetails();
        BeanUtils.copyProperties(chargeDetailsUpdateDTO, chargeDetails);
        //更新数据，并判断是否更新成功
        int rows = chargeDetailsMapper.updateById(chargeDetails);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
    /**
     * 分页查询收费明细列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<ChargeDetailsListVO> ChargeDetailsListVO分页对象
     */
    @Override
    public IPage<ChargeDetailsListVO> list(Integer pageNum, Integer pageSize, String communityCode) {
        Page<ChargeDetailsListVO> page = new Page<>(pageNum - 1, pageSize);
        return chargeDetailsMapper.selectPageVo(page, communityCode);
    }

    @Override
    public List<ChargeDetailsAmountVO> listAmount() {
        return chargeDetailsMapper.selectAmount();
    }

    @Override
    public List<ChargeDetailsDashboardVO> listDashboard() {
        return chargeDetailsMapper.selectDashboard();
    }
}




